package com.sc.nft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.BuyFirstUserDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.entity.BuyFirstUser;
import com.sc.nft.entity.DigitalCollectionProductInfo;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.AddBuyFirstUserExcelVO;
import com.sc.nft.entity.vo.AirdropUserListVO;
import com.sc.nft.entity.vo.BuyFirstUserListVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.BuyFirstUserService;
import com.sc.nft.sup.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 服务接口实现
 *
 * @author rdg
 * @since 2022-08-03 15:15:20
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BuyFirstUserServiceImpl implements BuyFirstUserService {
    private final BuyFirstUserDao buyFirstUserDao;
    private final UserInfoDao userInfoDao;

    @Override
    public Page<BuyFirstUserListVO> buyFirstUserList(Long productId, String userTel, int pageNo, int pageSize) {
        return buyFirstUserDao.buyFirstUserList(productId,userTel,pageNo,pageSize);
    }

    @Override
    public BuyFirstUser selectByProductIdAndUserId(Long productId, Long userId) {
        return buyFirstUserDao.selectByProductIdAndUserId(productId,userId);
    }

    @Override
    public List<AddBuyFirstUserExcelVO> addBuyFirstUserExcel(Long productId, MultipartFile uploadFile) throws IOException {
        List list=new ArrayList();
        List list1=new ArrayList();
        String fileName = uploadFile.getOriginalFilename();
        String substring = fileName.substring(fileName.lastIndexOf("."));
        InputStream inputStream = uploadFile.getInputStream();
        Workbook wb = null;
        if(".xls".equals(substring)){
            wb = new HSSFWorkbook(inputStream);
        }else if(".xlsx".equals(substring)){
            wb = new XSSFWorkbook(inputStream);
        }else {
            throw  new GlobalRunTimeException(ErrorCode.WRONG_FILE_TYPE);
        }
        Sheet sheet = wb.getSheetAt(0);
        //获取最大行数
        int rownum = sheet.getPhysicalNumberOfRows();
        AirdropUserListVO airdropUserListVO=new AirdropUserListVO();
        Boolean smFlag=false;
        for (int i = 1; i <rownum ; i++) {
            Row row = sheet.getRow(i);
            Cell cell = row.getCell(0);
            cell.setCellType(CellType.STRING);
            if (ObjectUtil.isNotEmpty(cell)){
                AddBuyFirstUserExcelVO addBuyFirstUserExcelVO=new AddBuyFirstUserExcelVO();
                String userTel = cell.getStringCellValue();
                UserInfo oneByUserTel = userInfoDao.getOneByUserTel(userTel);
                if (ObjectUtil.isNotEmpty(oneByUserTel)&&!list1.contains(userTel)){
                    BuyFirstUser buyFirstUsers = buyFirstUserDao.selectByProductIdAndUserId(productId, oneByUserTel.getId());
                    if (ObjectUtil.isEmpty(buyFirstUsers)){
                        addBuyFirstUserExcelVO.setNickName(oneByUserTel.getNickName());
                        addBuyFirstUserExcelVO.setUserId(oneByUserTel.getId());
                        addBuyFirstUserExcelVO.setUserTel(oneByUserTel.getUserTel());
                        list.add(addBuyFirstUserExcelVO);
                    }
                }
                list1.add(userTel);
            }
        }
        return list;
    }
}